package com.informationsystem.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.informationsystem.service.EventManager;
import com.informationsystem.service.SystemuserManager;
import com.informationsystem.dao.SearchException;
import com.informationsystem.model.Event;
import com.informationsystem.webapp.action.BaseAction;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class EventAction extends BaseAction implements Preparable {
    private EventManager eventManager;
    private List events;
    private Event event;
    private Long id;
    private String query;
    private Date hourformat;

    public void setEventManager(EventManager eventManager) {
        this.eventManager = eventManager;
    }

    public List getEvents() {
        return events;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String eventId = getRequest().getParameter("event.id");
            if (eventId != null && !eventId.equals("")) {
                event = eventManager.get(new Long(eventId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }

    public String list() {
        try {
            events = eventManager.search(query, Event.class);
        } catch (SearchException se) {
            addActionError(se.getMessage());
            events = eventManager.getAll();
        }
        return SUCCESS;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

    public String delete() {
    	event = eventManager.get(event.getId());
        eventManager.remove(event.getId());
        addLog("delete event id:" +event.getId() + " title: "+ event.getTitle());
        saveMessage(getText("event.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            event = eventManager.get(id);
        } else {
            event = new Event();
            event.setSystemuser(getLoginUser());
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (event.getId() == null);
        
        if(!isNew)
        	event.setPostDate(generatePostDate());
        else
        	event.setPostDate(new Date());
        
        event = eventManager.save(event);

        String key = (isNew) ? "event.added" : "event.updated";
        saveMessage(getText(key));

        if(!isNew){
        	addLog("edit event id:" +event.getId() + " title: "+ event.getTitle());
        }
        else{
        	addLog("add event id:" +event.getId() + " title: "+ event.getTitle());
        }
        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
    
    public SystemuserManager getSystemuserManager() {
		return systemuserManager;
	}

	public void setSystemuserManager(SystemuserManager systemuserManager) {
		this.systemuserManager = systemuserManager;
	}
	
	public List getSystemuserList(){
		return systemuserManager.getAll();
	}
	
	public List getEventTypeList(){
	    List eventType = new ArrayList();
	    eventType.add("Morning");
	    eventType.add("Afternoon");
	    eventType.add("Night");
	    eventType.add("All day");
	    return eventType;
	    
	}
	
	public Date getHourformat() {
		return event.getPostDate();
	}

	public void setHourformat(Date hourformat) {
		this.hourformat = hourformat;
	}
	
	@SuppressWarnings("deprecation")
	public Date generatePostDate(){
		Calendar cal = Calendar.getInstance();
		cal.setTime(event.getPostDate()); 
		cal.set(Calendar.HOUR_OF_DAY, hourformat.getHours());
		cal.set(Calendar.MINUTE,hourformat.getMinutes());

		return cal.getTime();
	}
	
}